﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PayexpDAL;
using Utilities;
using System.Globalization;
using PresentationControls;

namespace PayexpUserControls
{
    public partial class FilterControl : UserControl
    {
        private bool _resetDateFilterRadioButton = true;
        private bool _resetVoucherReceiptFilterRadioButton = true;
        private bool _resetDeletedTransactionsRadioButton = true;
        
        private bool _limitTheNumberOfRecords;

        public bool LimitTheNumberOfRecords
        {
            get { return _limitTheNumberOfRecords; }
            set { _limitTheNumberOfRecords = value; }
        }

        public bool OnlyPettyCashFilterCriteriaSelected
        {
            get
            {
                if (this.TransactionMasterIdList.Count == 0)
                {
                    return false;
                }
                else
                {
                    if (!this.checkBoxDateFilter.Checked &&
                        !this.checkBoxAmountRangeFilter.Checked &&
                        !this.checkBoxChequeNumberRangeFilter.Checked &&
                        !this.checkBoxHeadDetailsFilter.Checked &&
                        !this.checkBoxNarrationFilter.Checked &&
                        !this.checkBoxTransactionModeFilter.Checked &&
                        !this.checkBoxBankNameFilter.Checked &&
                        !this.checkBoxExpenseApproverFilter.Checked &&
                        !this.checkBoxVoucherReceiptFilter.Checked &&
                        !this.checkBoxDeletedTransactionsFilter.Checked)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        public FilterControl()
        {
            InitializeComponent();
            
           

            this.groupBoxDateFilters.Enabled = false;
            this.groupBoxAmountRangeFilters.Enabled = false;
            this.groupBoxChequeNumberRangeFilters.Enabled = false;
            this.groupBoxHeadDetailsFilters.Enabled = false;
            this.groupBoxNarrationFilters.Enabled = false;
            this.groupBoxTransactionModeFilter.Enabled = false;
            this.groupBoxBankNameFilter.Enabled = false;
            this.groupBoxExpenseApproverFilter.Enabled = false;
            this.groupBoxReceiptVoucherNumberFilter.Enabled = false;
            this.groupBoxDeletedTransactionsFilter.Enabled = false;

            List<ExpenseApprover> tempExpenseApproverList = new List<ExpenseApprover>();
            tempExpenseApproverList.AddRange(SettingsAndStaticListLoader.ExpenseApproverList);
            ListSelectionWrapper<ExpenseApprover> expenseApproverList = new ListSelectionWrapper<ExpenseApprover>(tempExpenseApproverList, "DisplayName");
            this.checkBoxComboBoxExpenseApproverFilter.DataSource = expenseApproverList;
            this.checkBoxComboBoxExpenseApproverFilter.DisplayMemberSingleItem = "Name";
            this.checkBoxComboBoxExpenseApproverFilter.DisplayMember = "NameConcatenated";
            this.checkBoxComboBoxExpenseApproverFilter.ValueMember = "Selected";

            List<BankAccount> tempBankAccountList = new List<BankAccount>();
            tempBankAccountList.AddRange(SettingsAndStaticListLoader.BankAccountList);
            ListSelectionWrapper<BankAccount> bankAccountList = new ListSelectionWrapper<BankAccount>(tempBankAccountList, "BankDisplayName");
            this.checkBoxComboBoxBankNameFilter.DataSource = bankAccountList;
            this.checkBoxComboBoxBankNameFilter.DisplayMemberSingleItem = "Name";
            this.checkBoxComboBoxBankNameFilter.DisplayMember = "NameConcatenated";
            this.checkBoxComboBoxBankNameFilter.ValueMember = "Selected";

            List<StaticList> tempTransactionModeList = new List<StaticList>();
            tempTransactionModeList.AddRange(SettingsAndStaticListLoader.TransactionModeList);
            ListSelectionWrapper<StaticList> transactionModeList = new ListSelectionWrapper<StaticList>(tempTransactionModeList, "ListItemValue");
            this.checkBoxComboBoxTransactionModeFilter.DataSource = transactionModeList;
            this.checkBoxComboBoxTransactionModeFilter.DisplayMemberSingleItem = "Name";
            this.checkBoxComboBoxTransactionModeFilter.DisplayMember = "NameConcatenated";
            this.checkBoxComboBoxTransactionModeFilter.ValueMember = "Selected";

            //this.groupBoxFilters.Text = "hehehe";
        }

        public bool OnlyReconciledRecords
        {
            get;
            set;
        }

        public bool OnlyUnreconciledRecords
        {
            get;
            set;
        }

        public bool OnlyIncomeRecords
        {
            get;
            set;
        }

        public bool OnlyExpenseRecords
        {
            get;
            set;
        }

        public bool ReconciliationMode
        {
            get;
            set;
        }

        private List<int> _transactionMasterIdList = new List<int>();

        public List<int> TransactionMasterIdList
        {
            get { return _transactionMasterIdList; }
            set { _transactionMasterIdList = value; }
        }

        public FilterCriteria GetFilterCriteria()
        {
            FilterCriteria filterCriteria = new FilterCriteria(this._limitTheNumberOfRecords);
            filterCriteria.ReconciliationMode = this.ReconciliationMode;
            PerformFilterCriteriaValidation(filterCriteria);
            return filterCriteria;
        }

        public void SetDateRange(DateTime from, DateTime to)
        {
            this.dateTimePickerTxnDateFromFilter.Value = from;
            this.dateTimePickerTxnDateToFilter.Value = to;
            this.checkBoxDateFilter.Checked = true;
        }

        public void Reset()
        {
            this.checkBoxDateFilter.Checked = false;
            this.checkBoxAmountRangeFilter.Checked = false;
            this.checkBoxChequeNumberRangeFilter.Checked = false;
            this.checkBoxHeadDetailsFilter.Checked = false;
            this.checkBoxNarrationFilter.Checked = false;
            this.checkBoxTransactionModeFilter.Checked = false;
            this.checkBoxBankNameFilter.Checked = false;
            this.checkBoxExpenseApproverFilter.Checked = false;
            this.checkBoxVoucherReceiptFilter.Checked = false;
            this.checkBoxDeletedTransactionsFilter.Checked = false;

            this.textBoxAmountFromFilter.Clear();
            this.textBoxAmountToFilter.Clear();

            this.textBoxChequeNumberFromFilter.Clear();
            this.textBoxChequeNumberToFilter.Clear();

            buttonClearFilterHeads_Click(null, null);

            this.textBoxNarrationFilter.Clear();

            this.textBoxVoucherReceiptFromFilter.Clear();
            this.textBoxVoucherReceiptToFilter.Clear();
        }



        private void checkBoxDateFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxDateFilters.Enabled = this.checkBoxDateFilter.Checked;

            if (this._resetDateFilterRadioButton)
            {
                this.radioBtnTransactionDateFilter.Checked = true;
            }
        }

        private void checkBoxAmountRangeFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxAmountRangeFilters.Enabled = this.checkBoxAmountRangeFilter.Checked;
        }

        private void checkBoxChequeNumberRangeFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxChequeNumberRangeFilters.Enabled = this.checkBoxChequeNumberRangeFilter.Checked;
        }

        private void checkBoxHeadDetailsFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxHeadDetailsFilters.Enabled = this.checkBoxHeadDetailsFilter.Checked;
        }

        private void checkBoxNarrationFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxNarrationFilters.Enabled = this.checkBoxNarrationFilter.Checked;
            if (this.checkBoxNarrationFilter.Checked)
            {
                this.textBoxNarrationFilter.Focus();
                this.textBoxNarrationFilter.Select(0, 0);
            }
        }

        private void checkBoxTransactionModeFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxTransactionModeFilter.Enabled = this.checkBoxTransactionModeFilter.Checked;
        }

        private void checkBoxBankNameFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxBankNameFilter.Enabled = this.checkBoxBankNameFilter.Checked;
        }

        private void checkBoxExpenseApproverFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxExpenseApproverFilter.Enabled = this.checkBoxExpenseApproverFilter.Checked;
        }

        private void checkBoxVoucherReceiptFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxReceiptVoucherNumberFilter.Enabled = this.checkBoxVoucherReceiptFilter.Checked;

            if (this._resetVoucherReceiptFilterRadioButton)
            {
                this.radioButtonReceiptFilter.Checked = true;
            }
        }

        private void checkBoxDeletedTransactionsFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxDeletedTransactionsFilter.Enabled = this.checkBoxDeletedTransactionsFilter.Checked;

            if (this._resetDeletedTransactionsRadioButton)
            {
                this.radioButtonIncludeDeletedTransactionsFilter.Checked = true;
            }
        }


        private void linkLabelFirstHeadDetailsFilter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SetHeadDetailsFilter(labelFirstHeadDetailsFilter);
        }

        private void linkLabelSecondHeadDetailsFilter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (string.IsNullOrEmpty(labelFirstHeadDetailsFilter.Text.Trim()))
            {
                SetHeadDetailsFilter(labelFirstHeadDetailsFilter);
            }
            else
            {
                SetHeadDetailsFilter(labelSecondHeadDetailsFilter);
            }
        }

        private void linkLabelThirdHeadDetailsFilter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (string.IsNullOrEmpty(labelFirstHeadDetailsFilter.Text.Trim()))
            {
                SetHeadDetailsFilter(labelFirstHeadDetailsFilter);
            }
            else if (string.IsNullOrEmpty(labelSecondHeadDetailsFilter.Text.Trim()))
            {
                SetHeadDetailsFilter(labelSecondHeadDetailsFilter);
            }
            else
            {
                SetHeadDetailsFilter(labelThirdHeadDetailsFilter);
            }
        }

        private void SetHeadDetailsFilter(Label headDetailsLabelToSet)
        {
            TransactionTypeTreeViewForm form = new TransactionTypeTreeViewForm(false, true, false);
            form.CreateTreeView(new List<string>(), true, true);

            DialogResult dr = form.ShowDialog();

            while (dr == System.Windows.Forms.DialogResult.OK && form.GetSelectedTransactionTypes().Count == 0)
            {
                MessageBox.Show("Please choose a valid head other than the root.", "Choose valid head", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dr = form.ShowDialog();
            }

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                headDetailsLabelToSet.Text = form.GetSelectedShortText();
                headDetailsLabelToSet.Tag = form.GetSelectedTransactionTypes();
            }
        }

        private void buttonClearFilterHeads_Click(object sender, EventArgs e)
        {
            this.labelFirstHeadDetailsFilter.Text = string.Empty;
            this.labelSecondHeadDetailsFilter.Text = string.Empty;
            this.labelThirdHeadDetailsFilter.Text = string.Empty;
        }

        private void PerformFilterCriteriaValidation(FilterCriteria filterCriteria)
        {
            bool anyCriteriaSelected = false;
            List<string> messages = new List<string>();

            #region Only Income Records
            if (this.OnlyIncomeRecords)
            {
                filterCriteria.OnlyIncomeRecords = true;
                anyCriteriaSelected = true;
            }
            #endregion Only Income Records

            #region Only Expense Records
            if (this.OnlyExpenseRecords)
            {
                filterCriteria.OnlyExpenseRecords = true;
                anyCriteriaSelected = true;
            }
            #endregion Only Expense Records

            #region Only Reconciled Records
            if (this.OnlyReconciledRecords)
            {
                filterCriteria.OnlyReconciledRecords = true;
                anyCriteriaSelected = true;
            }
            #endregion Only Reconciled Records

            #region Only Unreconciled Records
            if (this.OnlyUnreconciledRecords)
            {
                filterCriteria.OnlyUnreconciledRecords = true;
                anyCriteriaSelected = true;
            }
            #endregion Only Unreconciled Records

            #region Transaction Master Id List
            if (this.TransactionMasterIdList.Count > 0)
            {
                anyCriteriaSelected = true;
                filterCriteria.TransactionMasterIdList.Clear();
                filterCriteria.TransactionMasterIdList.AddRange(this.TransactionMasterIdList);
            }

            #endregion Transaction Master Id List

            #region date range validation
            if (this.checkBoxDateFilter.Checked)
            {
                anyCriteriaSelected = true;
                bool hasDateError = false;
                if (this.dateTimePickerTxnDateFromFilter.Value.Date.CompareTo(this.dateTimePickerTxnDateToFilter.Value.Date) > 0)
                {
                    messages.Add("From Date cannot be greater than To Date.");
                    hasDateError = true;
                }

                if (filterCriteria.LimitTheNumberOfRecords)
                {
                    //int maxDateRangeAllowed = int.Parse(AppSettingsLoader.AppSettingsValuesMap[Constants.AppSettingsKeys.MaxDateRangeInDays]);
                    string maxDateRangeSettingValue = SettingsAndStaticListLoader.GetAppSettingValue(Constants.AppSettingsKeys.MaxDateRangeInDays);
                    if (!string.IsNullOrEmpty(maxDateRangeSettingValue))
                    {
                        int maxDateRangeAllowed = int.Parse(maxDateRangeSettingValue);
                        if (this.dateTimePickerTxnDateToFilter.Value.Subtract(this.dateTimePickerTxnDateFromFilter.Value).TotalDays > (maxDateRangeAllowed - 1))
                        {
                            messages.Add(string.Format("Please limit your date range to {0} days inclusive of the 'From' and 'To' dates.", maxDateRangeAllowed));
                            hasDateError = true;
                        }
                    }
                }

                if (this.dateTimePickerTxnDateFromFilter.Value.Date.CompareTo(DateTime.Today.Date) > 0 ||
                         this.dateTimePickerTxnDateToFilter.Value.Date.CompareTo(DateTime.Today.Date) > 0)
                {
                    messages.Add("Please check the selected transaction dates. They cannot be later than today's date.");
                    hasDateError = true;
                }


                if (!hasDateError)
                {
                    filterCriteria.DateFrom = this.dateTimePickerTxnDateFromFilter.Value.Date;

                    filterCriteria.DateTo = this.dateTimePickerTxnDateToFilter.Value.Date;

                    filterCriteria.DateTo = filterCriteria.DateTo.Value.Add(new TimeSpan(0, 23, 59, 59, 999));

                    filterCriteria.TransactionDateSelected = this.radioBtnTransactionDateFilter.Checked;

                    //// set the bank accounts id
                    //if (!filterCriteria.TransactionDateSelected)
                    //{
                    //    ComboBoxItem selectedItem = (ComboBoxItem)this.comboBoxBankAccountsFilter.SelectedItem;
                    //    BankAccount selectedBankAccountFilter = (BankAccount)selectedItem.Value;
                    //    filterCriteria.BankAccountsId = selectedBankAccountFilter.Id;
                    //    filterCriteria.BankDisplayName = selectedBankAccountFilter.BankDisplayName;
                    //}
                }

                // if date range is given...then we do not have to worry about the number of records
                filterCriteria.PageNumber = null;
                filterCriteria.NumberOfRecords = null;
            }
            #endregion date range validation

            #region amount range validation
            if (this.checkBoxAmountRangeFilter.Checked)
            {
                anyCriteriaSelected = true;
                bool hasAmountRangeError = false;

                NumberFormatInfo nfiForAmount = new NumberFormatInfo(); // just two decimal places
                nfiForAmount.NumberDecimalDigits = 2;

                // From amount
                decimal transAmountValueFrom = 0;
                try
                {
                    decimal.TryParse(this.textBoxAmountFromFilter.Text.Trim(),
                                         NumberStyles.AllowDecimalPoint, nfiForAmount,
                                         out transAmountValueFrom);
                    if (transAmountValueFrom <= 0 || Utils.CountDecimalPlaces(transAmountValueFrom) > 2)
                    {
                        messages.Add("Please enter a valid value for amount in the 'From' field. All amount values should be non-negative and can have only 2 digits after the decimal.");
                        hasAmountRangeError = true;
                    }
                }
                catch
                {
                    messages.Add("Please enter a valid value for amount in the 'From' field. All amount values should be non-negative and can have only 2 digits after the decimal.");
                    hasAmountRangeError = true;
                }

                // To amount
                decimal transAmountValueTo = 0;
                try
                {
                    decimal.TryParse(this.textBoxAmountToFilter.Text.Trim(),
                                         NumberStyles.AllowDecimalPoint, nfiForAmount,
                                         out transAmountValueTo);
                    if (transAmountValueTo <= 0 || Utils.CountDecimalPlaces(transAmountValueTo) > 2)
                    {
                        messages.Add("Please enter a valid value for amount in the 'To' field. All amount values should be non-negative and can have only 2 digits after the decimal.");
                        hasAmountRangeError = true;
                    }
                }
                catch
                {
                    messages.Add("Please enter a valid value for amount in the 'To' field. All amount values should be non-negative and can have only 2 digits after the decimal.");
                    hasAmountRangeError = true;
                }

                if (transAmountValueFrom > transAmountValueTo)
                {
                    messages.Add("'From' amount value cannot be greater than 'To' amount value.");
                    hasAmountRangeError = true;
                }

                if (!hasAmountRangeError)
                {
                    filterCriteria.AmountFrom = transAmountValueFrom;
                    filterCriteria.AmountTo = transAmountValueTo;
                }
            }
            #endregion amount range validation

            #region cheque number validation
            if (this.checkBoxChequeNumberRangeFilter.Checked)
            {
                anyCriteriaSelected = true;
                bool hasChequeNumberRangeError = false;

                int chequeNumberFrom = 0;
                int chequeNumberTo = 0;

                int.TryParse(this.textBoxChequeNumberFromFilter.Text.Trim(), out chequeNumberFrom);
                if (chequeNumberFrom <= 0)
                {
                    messages.Add("Please enter a valid cheque number in the 'From' field.");
                    hasChequeNumberRangeError = true;
                }

                int.TryParse(this.textBoxChequeNumberToFilter.Text.Trim(), out chequeNumberTo);
                if (chequeNumberTo <= 0)
                {
                    messages.Add("Please enter a valid cheque number in the 'To' field.");
                    hasChequeNumberRangeError = true;
                }

                if (chequeNumberFrom > chequeNumberTo)
                {
                    messages.Add("Cheque number in the 'From' field cannot be greater than the cheque number in the 'To' field.");
                    hasChequeNumberRangeError = true;
                }

                if (!hasChequeNumberRangeError)
                {
                    filterCriteria.ChequeNumberFrom = chequeNumberFrom;
                    filterCriteria.ChequeNumberTo = chequeNumberTo;
                }

            }
            #endregion cheque number validation

            #region head details validation
            if (this.checkBoxHeadDetailsFilter.Checked)
            {
                anyCriteriaSelected = true;
                if (string.IsNullOrEmpty(this.labelFirstHeadDetailsFilter.Text.Trim()) &&
                string.IsNullOrEmpty(this.labelSecondHeadDetailsFilter.Text.Trim()) &&
                string.IsNullOrEmpty(this.labelThirdHeadDetailsFilter.Text.Trim()))
                {
                    messages.Add("Please select at least one 'head details'.");
                }
                else
                {
                    if (!string.IsNullOrEmpty(this.labelFirstHeadDetailsFilter.Text.Trim()) && this.labelFirstHeadDetailsFilter.Tag != null)
                    {
                        List<TransactionType> hierarchy = (List<TransactionType>)this.labelFirstHeadDetailsFilter.Tag;
                        filterCriteria.FirstHeadDetailsId = hierarchy.Last().Id;
                        filterCriteria.FirstHeadDetailsHierarchy = hierarchy;
                    }

                    if (!string.IsNullOrEmpty(this.labelSecondHeadDetailsFilter.Text.Trim()) && this.labelSecondHeadDetailsFilter.Tag != null)
                    {
                        List<TransactionType> hierarchy = (List<TransactionType>)this.labelSecondHeadDetailsFilter.Tag;
                        filterCriteria.SecondHeadDetailsId = hierarchy.Last().Id;
                        filterCriteria.SecondHeadDetailsHierarchy = hierarchy;
                    }

                    if (!string.IsNullOrEmpty(this.labelThirdHeadDetailsFilter.Text.Trim()) && this.labelThirdHeadDetailsFilter.Tag != null)
                    {
                        List<TransactionType> hierarchy = (List<TransactionType>)this.labelThirdHeadDetailsFilter.Tag;
                        filterCriteria.ThirdHeadDetailsId = hierarchy.Last().Id;
                        filterCriteria.ThirdHeadDetailsHierarchy = hierarchy;
                    }

                }
            }
            #endregion head details validation

            #region narration validation

            if (this.checkBoxNarrationFilter.Checked)
            {
                anyCriteriaSelected = true;
                if (string.IsNullOrEmpty(this.textBoxNarrationFilter.Text.Trim()))
                {
                    messages.Add("Please either enter a narration text to look for or uncheck the narration filter.");
                }
                else
                {
                    filterCriteria.Narration = this.textBoxNarrationFilter.Text.Trim().Replace(Environment.NewLine, string.Empty);
                }
            }

            #endregion narration validation

            #region transaction mode validation
            if (this.checkBoxTransactionModeFilter.Checked)
            {
                anyCriteriaSelected = true;
                ListSelectionWrapper<StaticList> transactionModeList = (ListSelectionWrapper<StaticList>)this.checkBoxComboBoxTransactionModeFilter.DataSource;
                if (!transactionModeList.Any(t => t.Selected))
                {
                    messages.Add("Please either select one or more transaction modes or uncheck the Transaction Mode filter.");
                }
                else
                {
                    filterCriteria.TransactionModeList.AddRange(transactionModeList.Where(t => t.Selected).Select(r => r.Item));
                }
            }
            #endregion  transaction mode validation

            #region bank name validation
            if (this.checkBoxBankNameFilter.Checked)
            {
                anyCriteriaSelected = true;
                ListSelectionWrapper<BankAccount> bankAccountList = (ListSelectionWrapper<BankAccount>)this.checkBoxComboBoxBankNameFilter.DataSource;
                if (!bankAccountList.Any(t => t.Selected))
                {
                    
                    messages.Add("Please either select one or more bank names or uncheck the Bank Name filter.");
                    
                }
                else
                {
                    filterCriteria.BankAccountsList.AddRange(bankAccountList.Where(t => t.Selected).Select(r => r.Item));
                }
            }
            #endregion bank name validation

            #region expense approver validation
            if (this.checkBoxExpenseApproverFilter.Checked)
            {
                anyCriteriaSelected = true;
                ListSelectionWrapper<ExpenseApprover> expenseApproverList = (ListSelectionWrapper<ExpenseApprover>)this.checkBoxComboBoxExpenseApproverFilter.DataSource;
                if (!expenseApproverList.Any(t => t.Selected))
                {
                    messages.Add("Please either select one or more expense approver names or uncheck the Expense Approver filter.");
                }
                else
                {
                    filterCriteria.ExpenseApproverList.AddRange(expenseApproverList.Where(t => t.Selected).Select(r => r.Item));
                }
            }
            #endregion expense approver validation

            #region voucher-receipt number validation
            if (this.checkBoxVoucherReceiptFilter.Checked)
            {
                anyCriteriaSelected = true;

                bool hasVoucherReceiptNumberRangeError = false;

                int voucherReceiptNumberFrom = 0;
                int voucherReceiptNumberTo = 0;

                int.TryParse(this.textBoxVoucherReceiptFromFilter.Text.Trim(), out voucherReceiptNumberFrom);
                if (voucherReceiptNumberFrom <= 0)
                {
                    messages.Add("Please enter a valid voucher/ receipt number in the 'From' field");
                    hasVoucherReceiptNumberRangeError = true;
                }

                int.TryParse(this.textBoxVoucherReceiptToFilter.Text.Trim(), out voucherReceiptNumberTo);
                if (voucherReceiptNumberTo <= 0)
                {
                    messages.Add("Please enter a valid voucher/ receipt number in the 'To' field");
                    hasVoucherReceiptNumberRangeError = true;
                }

                if (voucherReceiptNumberFrom > voucherReceiptNumberTo)
                {
                    messages.Add("Voucher/ Receipt number in the 'From' field cannot be greater than the number in the 'To' field.");
                    hasVoucherReceiptNumberRangeError = true;
                }

                if (!hasVoucherReceiptNumberRangeError)
                {
                    filterCriteria.VoucherReceiptFrom = voucherReceiptNumberFrom;
                    filterCriteria.VoucherReceiptTo = voucherReceiptNumberTo;
                    filterCriteria.IsVoucherSelected = this.radioButtonVoucherFilter.Checked;
                }
                else
                {
                    filterCriteria.IsVoucherSelected = null;
                }

            }
            #endregion voucher-receipt number validation

            #region Include Deleted Records

            if (this.checkBoxDeletedTransactionsFilter.Checked)
            {
                anyCriteriaSelected = true;
                if (!this.radioButtonIncludeDeletedTransactionsFilter.Checked && !this.radioButtonDisplayOnlyDeletedTransactionsFilter.Checked)
                {
                    messages.Add("Please either select one of deleted transactions option or uncheck the Deleted Transactions filter.");
                    filterCriteria.IncludeDeletedTransactions = null;
                }
                else
                {
                    filterCriteria.IncludeDeletedTransactions = this.radioButtonIncludeDeletedTransactionsFilter.Checked;
                }
            }

            #endregion Include Deleted Records

            if (!anyCriteriaSelected)
            {
                messages.Add("Please select at least one criteria");
            }
            
            filterCriteria.ValidationErrorList = new List<string>();
            if (messages.Count > 0)
            {
                filterCriteria.ValidationErrorList.AddRange(messages);
                filterCriteria.HasError = true;
            }
            else
            {
                filterCriteria.HasError = false;
            }
        }


        private void radioBtnTransactionDateFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioBtnBankDateFilter.Checked = !this.radioBtnTransactionDateFilter.Checked;
            this._resetDateFilterRadioButton = false;
        }

        private void radioBtnBankDateFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioBtnTransactionDateFilter.Checked = !this.radioBtnBankDateFilter.Checked;
            this._resetDateFilterRadioButton = false;
        }

        private void radioButtonVoucherFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioButtonReceiptFilter.Checked = !this.radioButtonVoucherFilter.Checked;
            this._resetVoucherReceiptFilterRadioButton = false;
        }

        private void radioButtonReceiptFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioButtonVoucherFilter.Checked = !this.radioButtonReceiptFilter.Checked;
            this._resetVoucherReceiptFilterRadioButton = false;
        }

        private void radioButtonIncludeDeletedTransactionsFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioButtonDisplayOnlyDeletedTransactionsFilter.Checked = !this.radioButtonIncludeDeletedTransactionsFilter.Checked;
            this._resetDeletedTransactionsRadioButton = false;
        }

        private void radioButtonDisplayOnlyDeletedTransactionsFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.radioButtonIncludeDeletedTransactionsFilter.Checked = !this.radioButtonDisplayOnlyDeletedTransactionsFilter.Checked;
            this._resetDeletedTransactionsRadioButton = false;
        }
    }
}
